home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 2 / Macwelt DVD 2.cdr / System / Internet-Utilities / macosx / News Mac 1.1.dmg / ChannelManager.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-04-08  |  8.8 KB  |  239 lines

  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dialog;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.awt.Point;
  9. import java.awt.Toolkit;
  10. import java.awt.Window;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.KeyEvent;
  14. import java.awt.event.KeyListener;
  15. import javax.swing.AbstractButton;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JButton;
  18. import javax.swing.JCheckBox;
  19. import javax.swing.JComponent;
  20. import javax.swing.JDialog;
  21. import javax.swing.JLabel;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25. import javax.swing.border.EmptyBorder;
  26. import javax.swing.border.MatteBorder;
  27.  
  28. public class ChannelManager extends JDialog implements ActionListener, KeyListener {
  29.    protected ChannelList database = new ChannelList();
  30.    protected int selectedItem = 0;
  31.    protected JButton updateButton = new JButton(new ImageIcon("newsmac.app/contents/resources/update-channel.gif"));
  32.    protected JLabel categoryIcon = new JLabel(new ImageIcon());
  33.    protected JLabel channelName = new JLabel();
  34.    protected JTextArea channelDescription = new JTextArea();
  35.    protected JScrollPane scrollArea = new JScrollPane(20, 31);
  36.    protected Config theConfig;
  37.    protected Container main = new Container();
  38.    protected NewsMac parent;
  39.  
  40.    public ChannelManager(NewsMac theParent, Config conf) {
  41.       super(theParent, "Channel Manager", true);
  42.       this.parent = theParent;
  43.       this.database = this.parent.theEngine.getChannels();
  44.       this.theConfig = conf;
  45.       Toolkit theKit = ((Window)this).getToolkit();
  46.       int width = 530;
  47.       int height = 388;
  48.       int xPos = (int)(new Dimension(theKit.getScreenSize())).getWidth() / 2 - width / 2;
  49.       ((Component)this).setBounds(xPos, 160, width, height);
  50.       ((Dialog)this).setResizable(false);
  51.       JLabel columnHeaders = new JLabel(new ImageIcon("newsmac.app/contents/resources/columns.jpg"));
  52.       ((Component)columnHeaders).setBounds(20, 20, width - 40, 21);
  53.       this.scrollArea.setBounds(20, 39, 490, 236);
  54.       this.scrollArea.setBorder(new MatteBorder(0, 1, 1, 1, new Color(150, 150, 150)));
  55.       this.scrollArea.setViewportView(this.channelList(this.database));
  56.       this.categoryIcon.setBounds(20, 287, 32, 32);
  57.       this.channelName = AquaUtil.aquaSmallBoldLabel(60, 289, "No channel selected", 2);
  58.       this.channelDescription.setBounds(60, 305, 246, 42);
  59.       this.channelDescription.setFont(AquaUtil.SMALL_SYSTEM);
  60.       this.channelDescription.setText("");
  61.       this.channelDescription.setOpaque(false);
  62.       this.channelDescription.setEditable(false);
  63.       this.channelDescription.setLineWrap(true);
  64.       this.channelDescription.setWrapStyleWord(true);
  65.       this.channelDescription.setBorder(new EmptyBorder(0, 0, 0, 0));
  66.       this.updateButton.setBounds(448, 288, 64, 64);
  67.       this.updateButton.setVerticalAlignment(0);
  68.       this.updateButton.setHorizontalAlignment(0);
  69.       this.updateButton.setVerticalTextPosition(3);
  70.       this.updateButton.setHorizontalTextPosition(0);
  71.       this.updateButton.setText("Update");
  72.       this.updateButton.setEnabled(true);
  73.       this.updateButton.addActionListener(this);
  74.       this.main.add(columnHeaders);
  75.       this.main.add(this.scrollArea);
  76.       this.main.add(this.categoryIcon);
  77.       this.main.add(this.channelName);
  78.       this.main.add(this.channelDescription);
  79.       this.main.add(this.updateButton);
  80.       ((JDialog)this).getContentPane().add(this.main);
  81.       ((Component)this).addKeyListener(this);
  82.       this.updateChannelInfo(this.database.getChannel(0));
  83.    }
  84.  
  85.    protected JPanel channelList(ChannelList theList) {
  86.       JPanel temp = new JPanel(new BorderLayout());
  87.       Container holder = new Container();
  88.       int y = 0;
  89.  
  90.       for(int i = 0; i < theList.size(); ++i) {
  91.          if (i == this.selectedItem) {
  92.             holder.add(this.row(0, y, i, theList.getChannel(i), true));
  93.          } else {
  94.             holder.add(this.row(0, y, i, theList.getChannel(i), false));
  95.          }
  96.  
  97.          y += 20;
  98.       }
  99.  
  100.       ((Component)holder).setBounds(0, 0, 450, y);
  101.       ((JComponent)temp).setBackground(new Color(255, 255, 255));
  102.       ((JComponent)temp).setBorder(new EmptyBorder(0, 0, 0, 0));
  103.       ((Container)temp).add(holder, "Center");
  104.       ((JComponent)temp).setPreferredSize(new Dimension(((Component)holder).getWidth(), ((Component)holder).getHeight()));
  105.       return temp;
  106.    }
  107.  
  108.    protected Container row(int x, int y, int n, Channel theChannel, boolean selected) {
  109.       Container temp = new Container();
  110.       JButton bg = new JButton();
  111.       JCheckBox on = new JCheckBox();
  112.       JLabel channelName = AquaUtil.aquaSmallLabel(54, 3, theChannel.getName(), 2);
  113.       JLabel version = AquaUtil.aquaSmallLabel(440, 3, "" + theChannel.getVersion(), 2);
  114.       JLabel cat = AquaUtil.aquaSmallLabel(296, 3, Channel.CATEGORY[theChannel.getCategory()], 2);
  115.       ((Component)bg).setBounds(0, 0, 480, 20);
  116.       ((JComponent)bg).setBorder(new EmptyBorder(0, 0, 0, 0));
  117.       ((AbstractButton)bg).setActionCommand(theChannel.getName());
  118.       ((AbstractButton)bg).addActionListener(this);
  119.       if (selected) {
  120.          ((JComponent)bg).setBackground(Config.SELECTION_COLOUR);
  121.          ((JComponent)channelName).setForeground(new Color(255, 255, 255));
  122.          ((JComponent)version).setForeground(new Color(255, 255, 255));
  123.          ((JComponent)cat).setForeground(new Color(255, 255, 255));
  124.       } else if (n % 2 == 0) {
  125.          ((JComponent)bg).setBackground(new Color(255, 255, 255));
  126.       } else {
  127.          ((JComponent)bg).setBackground(new Color(239, 247, 255));
  128.       }
  129.  
  130.       ((AbstractButton)on).setSelected(theChannel.isOn());
  131.       ((Component)on).setBounds(16, 1, 18, 18);
  132.       ((JComponent)on).setOpaque(false);
  133.       ((AbstractButton)on).setActionCommand(theChannel.getName() + "on");
  134.       ((AbstractButton)on).addActionListener(this);
  135.       temp.add(on);
  136.       temp.add(channelName);
  137.       temp.add(version);
  138.       temp.add(cat);
  139.       temp.add(bg);
  140.       ((Component)temp).setBounds(x, y, 480, 20);
  141.       return temp;
  142.    }
  143.  
  144.    private void updateChannelInfo(Channel theChannel) {
  145.       this.channelName.setText(theChannel.getName());
  146.       String[] channelIcon = new String[]{"toolbar-mynews.gif", "toolbar-macnews.gif", "toolbar-specialist.gif", "toolbar-industry.gif", "toolbar-world.gif"};
  147.       this.categoryIcon.setIcon(new ImageIcon("newsmac.app/contents/resources/" + channelIcon[theChannel.getCategory()]));
  148.       String temp = theChannel.getDescription();
  149.       if (temp.length() > 128) {
  150.          temp = temp.substring(0, 104) + "...";
  151.       }
  152.  
  153.       this.channelDescription.setText(temp);
  154.    }
  155.  
  156.    public void paint(Graphics g) {
  157.       super.paint(g);
  158.    }
  159.  
  160.    public static void main(String[] args) {
  161.    }
  162.  
  163.    public void keyTyped(KeyEvent kv) {
  164.    }
  165.  
  166.    public void keyReleased(KeyEvent kv) {
  167.    }
  168.  
  169.    public void keyPressed(KeyEvent kv) {
  170.       int key = kv.getKeyCode();
  171.       if (key == 40) {
  172.          if (this.selectedItem < this.database.size() - 1) {
  173.             ++this.selectedItem;
  174.          }
  175.  
  176.          Point old = this.scrollArea.getViewport().getViewPosition();
  177.          if (old.getY() + (double)20.0F <= (double)this.scrollArea.getViewport().getView().getHeight()) {
  178.             old.setLocation(old.getX(), old.getY() + (double)20.0F);
  179.          }
  180.  
  181.          this.scrollArea.setViewportView(this.channelList(this.database));
  182.          this.scrollArea.getViewport().setViewPosition(old);
  183.          this.updateChannelInfo(this.database.getChannel(this.selectedItem));
  184.       }
  185.  
  186.       if (key == 38) {
  187.          if (this.selectedItem > 0) {
  188.             --this.selectedItem;
  189.          }
  190.  
  191.          Point old = this.scrollArea.getViewport().getViewPosition();
  192.          if (old.getY() - (double)20.0F >= (double)0.0F) {
  193.             old.setLocation(old.getX(), old.getY() - (double)20.0F);
  194.          }
  195.  
  196.          this.scrollArea.setViewportView(this.channelList(this.database));
  197.          this.scrollArea.getViewport().setViewPosition(old);
  198.          this.updateChannelInfo(this.database.getChannel(this.selectedItem));
  199.       }
  200.  
  201.    }
  202.  
  203.    public void actionPerformed(ActionEvent newEvent) {
  204.       String buttonString = newEvent.getActionCommand();
  205.       if (buttonString.equals("Update")) {
  206.          this.updateButton.setEnabled(false);
  207.          this.updateButton.setDisabledIcon(new ImageIcon("newsmac.app/contents/resources/updating.gif"));
  208.  
  209.          try {
  210.             Thread checkChannels = new Updater(this.parent, this);
  211.             checkChannels.start();
  212.          } catch (Exception var5) {
  213.             System.out.println("ChannelManager: " + var5);
  214.          }
  215.       }
  216.  
  217.       if (!buttonString.equals("")) {
  218.          for(int i = 0; i < this.database.size(); ++i) {
  219.             if (buttonString.equals(this.database.getChannel(i).getName())) {
  220.                this.selectedItem = i;
  221.                Point old = this.scrollArea.getViewport().getViewPosition();
  222.                this.scrollArea.setViewportView(this.channelList(this.database));
  223.                this.scrollArea.getViewport().setViewPosition(old);
  224.                this.updateChannelInfo(this.database.getChannel(i));
  225.             }
  226.  
  227.             if (buttonString.equals(this.database.getChannel(i).getName() + "on")) {
  228.                if (this.database.getChannel(i).isOn()) {
  229.                   this.database.getChannel(i).setOn(false);
  230.                } else {
  231.                   this.database.getChannel(i).setOn(true);
  232.                }
  233.             }
  234.          }
  235.       }
  236.  
  237.    }
  238. }
  239.